/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  tempdb.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(30/04/25)
 *         Author:  chenhongyu0822@163.com
 *      ChangeLog:  1, Release initial version on "30/04/25 22:13:00"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>


#include "getdata.h"
#include "logger.h"

int tempdb_init(sqlite3 **db, char *file, char *table_name)
{
      char              *zErrMsg=0;
      int               rc;
      char              sql[1024];

      if(sqlite3_open(file, db))
      {
		  log_error("Can't open tempdb:%s\n", sqlite3_errmsg(*db));
          return -1; 
      }   
      else
      {   
		  log_info("Open tempdb successfully!\n");
	  }

	  snprintf(sql,sizeof(sql),
			"CREATE TABLE IF NOT EXISTS %s("
			"RPI          TEXT      NOT NULL,"
			"TEMP         REAL      NOT NULL,"
			"TIME         TEXT      NOT NULL);", 
			table_name);

      rc=sqlite3_exec(*db, sql, 0, 0, &zErrMsg);
      if(rc!=SQLITE_OK)
      {
         log_error("Create table failure:%s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         return -2; 
      }   
      else
      {  
		 log_info("Table created successfully!\n");
      }   
      return 0;
 }

int insert_data(sqlite3 *db, char *table_name, Sample_Data_t Data)
{
    char                    *errMsg;
    char                    sql[1024];

    snprintf(sql, sizeof(sql), "INSERT INTO %s (rpi,temp,time) VALUES ('%s','%.2f','%s'); ",
		table_name,	Data.rpi,Data.temp,Data.time);

    if(sqlite3_exec(db, sql, 0, 0, &errMsg)!=SQLITE_OK)
    {
		log_error("SQL error:%s\n", errMsg);
        sqlite3_free(errMsg);
        return -1;
    }
    return 0;
}

int is_tempdb_empty(sqlite3 *db, char *table_name)
{
	int               count=0;
	sqlite3_stmt      *stmt;
    char              sql[256];

	snprintf(sql,sizeof(sql), "SELECT COUNT(*)FROM %s", table_name);

	if(sqlite3_prepare_v2(db, sql, -1, &stmt, NULL)==SQLITE_OK)
	{
		if(sqlite3_step(stmt)==SQLITE_ROW)
		{
			count=sqlite3_column_int(stmt, 0);
		}
	}
	sqlite3_finalize(stmt);
	return count;
}


int fetch_data(sqlite3 *db, char *table_name,Sample_Data_t *data)
{
    char                     sql[256];
	sqlite3_stmt             *stmt;
	int                      rc;
	
	snprintf(sql,sizeof(sql),"SELECT rpi, temp, time FROM %s ORDER BY time ASC LIMIT 1;",table_name);

	if((sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))!=SQLITE_OK)
    {
		log_error("SQL error:%s\n", sqlite3_errmsg(db));
        return -1;
    }

	if((rc=sqlite3_step(stmt))==SQLITE_ROW)
	{
		strncpy(data->rpi, (const char *)sqlite3_column_text(stmt, 0), sizeof(data->rpi));
		data->temp = sqlite3_column_double(stmt, 1);
		strncpy(data->time, (const char *)sqlite3_column_text(stmt, 2), sizeof(data->time));
	}
	else
	{
		log_error("No data found\n");
		sqlite3_finalize(stmt);
		return -2;
	}

	sqlite3_finalize(stmt);
	return 0;
}

int delete_data(sqlite3 *db, const char *table_name, Sample_Data_t data)
{
	char sql[256];
	char *errMsg;

	snprintf(sql, sizeof(sql), "DELETE FROM %s WHERE time = '%s';", table_name, data.time);

	if (sqlite3_exec(db, sql, 0, 0, &errMsg) != SQLITE_OK)
	{
		log_error("Delete data failure: %s\n", errMsg);
		sqlite3_free(errMsg);
		return -1;
	}
	return 0;
}

